package com.jtpay.sdk.weight;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jtpay.sdk.R;

import java.util.List;

import apiUtils.models.PayAppInfo;

/**
 * Created by Administrator on 2017/8/28.
 */

public class AppNamePop {
    private List<PayAppInfo> mPayAppInfos;
    private Context mContext;
    private PopupWindow mPopupWindow;
    private ListView mListView;
    private TextView mTextViewItem;

    public AppNamePop(List<PayAppInfo> payAppInfos, Context context) {
        mPayAppInfos = payAppInfos;
        mContext = context;
        View view = LayoutInflater.from(context).inflate(R.layout.pop_appname, null);
        mListView = (ListView) view.findViewById(R.id.listView);
        mListView.setAdapter(new PopAdapter());
        mListView.setFocusableInTouchMode(true);
        mListView.setFocusable(true);
        mPopupWindow = new PopupWindow(view, context.getResources().getDimensionPixelSize(R.dimen.popmenu_width), context.getResources().getDimensionPixelSize(R.dimen.popmenu_width));
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListView.setOnItemClickListener(listener);
    }

    public void showAsDropDown(View parent) {
        mPopupWindow.showAsDropDown(parent, 0, 0);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.update();
    }

    public void dismiss() {
        mPopupWindow.dismiss();
    }

    private final class PopAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mPayAppInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return mPayAppInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pop_appname_item, null);
            mTextViewItem = (TextView) convertView.findViewById(R.id.textView_item);
            mTextViewItem.setText(mPayAppInfos.get(position).getAppName());
            return convertView;
        }
    }
}
